﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace ERPStore.Diagnostics.Controllers
{
	public class DiagnosticApiController : ApiController
	{
		public DiagnosticApiController(Services.RewriterService rewriterService)
		{
			this.RewriterService = rewriterService;
		}

		protected Services.RewriterService RewriterService { get; private set; }

		[HttpGet]
		public DateTime Ping()
		{
			return DateTime.Now;
		}

		[ERPStore.Web.Controllers.ActionFilters.ApiAuthorizedOperation]
		public void PutRewriteUrl([FromBody]Models.RewriteUrl model)
		{
			RewriterService.SaveRewriteUrl(model);
		}

		[ERPStore.Web.Controllers.ActionFilters.ApiAuthorizedOperation]
		public Dictionary<string, string> GetERPStoreRouteList()
		{
			var result = new Dictionary<string, string>();
			foreach (var item in System.Web.Routing.RouteTable.Routes)
			{
				var route = item as ERPStore.Web.Routing.ERPStoreRoute;
				if (route == null)
				{
					continue;
				}
				result.Add(route.Name, route.Url);
			}
			return result;
		}
	}
}